{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Lesson 04"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 题目1： 计算倒计时，现在距离2018元旦距离现在还有多少天"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "177\n",
      "<class 'datetime.datetime'>\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "#日期格式转换\n",
    "newyear = datetime.strptime('2018-01-01', '%Y-%m-%d')\n",
    "#获取现在的时间\n",
    "now = datetime.now()\n",
    "#时间差\n",
    "timedelta = newyear-now\n",
    "print(timedelta.days)\n",
    "print(type(newyear))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 题目2： 计算一个正整数的因数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def func(number):\n",
    "    b = []\n",
    "    for i in range(1,number+1):\n",
    "        if number % i == 0:\n",
    "            print(i)\n",
    "            b.append(i)\n",
    "    return b\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "5\n",
      "10\n",
      "[1, 2, 5, 10]\n"
     ]
    }
   ],
   "source": [
    "print(func(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 题目3： 写一个寻找列表中最大数的函数（不用列表排序方法）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n",
      "-3\n"
     ]
    }
   ],
   "source": [
    "# 找到最大数 #1\n",
    "\n",
    "def find_max(l):\n",
    "    \n",
    "    # max = float('-inf') 表示负无穷大\n",
    "    max = float('-inf')\n",
    "    for x in l:\n",
    "        if x > max:\n",
    "            max = x\n",
    "    return max\n",
    "\n",
    "print(find_max([-20,1,6,7,20,5]))\n",
    "print(find_max([-20,-3,-6,-7,-5]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n"
     ]
    }
   ],
   "source": [
    "# 找到最大数 #2\n",
    "# 使用递归\n",
    "\n",
    "def find_max(l):\n",
    "    if len(l) == 1:\n",
    "        return l[0]\n",
    "    v1 = l[0]\n",
    "    v2 = find_max(l[1:])\n",
    "    if v1 > v2:\n",
    "        return v1\n",
    "    else:\n",
    "        return v2\n",
    "\n",
    "print(find_max([1,6,7,20,5]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 题目4：函数可变参数练习1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0\n",
      "2.5\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "# kind 中 增加 max key， \n",
    "# max = ingnore， 则忽略最大值\n",
    "def cal_6(*numbers, **kind):\n",
    "    \n",
    "    if 'kind' in kind:\n",
    "        kind_value = kind.get('kind')\n",
    "        \n",
    "    if 'max' in kind:\n",
    "        if kind.get('max') == 'ignore':\n",
    "            numbers = list(numbers)\n",
    "            numbers.remove(max(numbers))\n",
    "    \n",
    "    n = 0\n",
    "    for i in numbers:\n",
    "        n = n + i\n",
    "        \n",
    "    if kind_value == 'avg':\n",
    "        return n / len(numbers)\n",
    "    if kind_value == 'sum':\n",
    "        return n\n",
    "    \n",
    "print(cal_6(1,2,3,4, kind='avg', max='ignore',min='ignore'))\n",
    "print(cal_6(1,2,3,4, kind='avg'))\n",
    "print(cal_6(1,2,3,4, kind='sum'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 题目5：函数可变参数练习2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.75\n",
      "2.5\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "# kind 中 增加 min key，\n",
    "# min key = double, 则最小值计算两次\n",
    "\n",
    "def cal_7(*numbers, **kw):\n",
    "    \n",
    "    numbers = list(numbers)\n",
    "    \n",
    "    if 'kind' in kw:\n",
    "        kind_value = kw.get('kind')\n",
    "        \n",
    "    if 'max' in kw:\n",
    "        if kw.get('max') == 'ignore':\n",
    "            numbers.remove(max(numbers))\n",
    "\n",
    "    if 'min' in kw:\n",
    "        if kw.get('min') == 'double':\n",
    "            numbers.append(min(numbers))\n",
    "            \n",
    "    n = 0\n",
    "    for i in numbers:\n",
    "        n = n + i\n",
    "        \n",
    "    if kind_value == 'avg':\n",
    "        return n / len(numbers)\n",
    "    if kind_value == 'sum':\n",
    "        return n\n",
    "    \n",
    "print(cal_7(1,2,3,4, kind='avg', max='ignore', min='double'))\n",
    "print(cal_7(1,2,3,4, kind='avg'))\n",
    "print(cal_7(1,2,3,4, kind='sum'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
